<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar title="个人信息" class="page-nav-bar" left-arrow @click-left="$router.back()"></van-nav-bar>
    <!-- 个人信息 -->
    <!-- 操作dom元素用 选择图片时的文件框 -->
    <input type="file" hidden ref="file" @change="onFileChange">
    <van-cell title="头像" is-link @click="$refs.file.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell title="昵称" :value="user.name" is-link @click="isUpdateNmaeShow=true"> </van-cell>
    <van-cell title="性别" :value="user.gender === 0 ? '男' : '女'" is-link @click="isUpdateGenderShow=true">
    </van-cell>
    <van-cell title="生日" :value="birthday" is-link @click="isUpdateBirthdayShow=true">

    </van-cell>

    <!-- 修改昵称弹出层 -->
    <van-popup v-model="isUpdateNmaeShow" style="height: 100%;" position="bottom">
      <update-name v-if="isUpdateNmaeShow" @close="isUpdateNmaeShow=false" v-model="user.name"></update-name>
    </van-popup>
    <!-- 修改性别弹出层 -->
    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <update-gender v-if="isUpdateGenderShow" @close="isUpdateGenderShow=false" v-model="user.gender"></update-gender>
    </van-popup>
    <!-- 修改生日弹出层 -->
    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
     <update-birthday v-if="isUpdateBirthdayShow" v-model="user.birthday" @close="isUpdateBirthdayShow=false"></update-birthday>
    </van-popup>
    <!-- 修改图像弹出层 -->
    <van-popup v-model="isUpdatePhotoShow" style="hight:100%" position="top" @update-photo="user.photo=$event">
     <update-photo v-if="isUpdatePhotoShow" :img="img" @close="isUpdatePhotoShow=false" @update-photo="user.photo=$event"></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import UpdateName from '@/views/user-profile/component/update-name.vue'
import UpdateGender from '@/views/user-profile/component/update-gender.vue'
import UpdateBirthday from '@/views/user-profile/component/update-birthday.vue'
import UpdatePhoto from '@/views/user-profile/component/update-photo.vue'
export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data() {
    return {
      user: {}, //用户个人信息
      birthday: '',
      isUpdateNmaeShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow:false,
      img:null //预览的图片
    }
  },
  created() {
    this.loadUserProfile()
  },
  mounted() {},

  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        // console.log(data);
        this.user = data.data
        this.birthday = this.user.birthday.slice(0, 10)
      } catch (error) {
        this.$toast('失败')
      }
    },
    onFileChange(){
      //获取文件对象
      const file=this.$refs.file.files[0]
      // 基于文章对象获取blob数据  
      // 设置图片的 src
    this.img= window.URL.createObjectURL(file)
    //  console.log(img);
    //图像弹出层显示
    this.isUpdatePhotoShow=true
     // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value=''
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  :deep(.van-icon-arrow-left) {
    color: #fff;
  }
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: rgb(225, 219, 219);
  }
}
</style>
